using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.Business;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Business.Rules;
using ACME.Northwind.Business.SelectCommands;
using System.Collections.Generic;

namespace ACME.Northwind.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'Territory' object
	/// </summary>
	[Serializable()]
	partial class DomainTerritory : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "TerritoryCollection";
		internal DomainTerritoryCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainTerritory(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainTerritoryCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal TerritoryPrimaryKey PrimaryKey
		{
			get { return new TerritoryPrimaryKey(this.TerritoryId); }
		}

		internal int RegionId
		{
			get
			{
				try
				{
					return ((int)(base[this.ParentCol.RegionIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (!base[this.ParentCol.RegionIdColumn].Equals(value))
				{
					base[this.ParentCol.RegionIdColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					TerritoryCollection list = (TerritoryCollection)this.ParentCol.SubDomain[Collections.TerritoryCollection];
					Territory item = list.GetItemByPK(this.TerritoryId);
					if (item != null) item.RegionId = value;
				}
			}
		}

		internal string TerritoryDescription
		{
			get
			{
				try
				{
					if (base[this.ParentCol.TerritoryDescriptionColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.TerritoryDescriptionColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.TerritoryDescriptionColumn].Equals(value))
				{
					base[this.ParentCol.TerritoryDescriptionColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					TerritoryCollection list = (TerritoryCollection)this.ParentCol.SubDomain[Collections.TerritoryCollection];
					Territory item = list.GetItemByPK(this.TerritoryId);
					if (item != null) item.TerritoryDescription = value;
				}
			}
		}

		internal string TerritoryId
		{
			get
			{
				try
				{
					if (base[this.ParentCol.TerritoryIdColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.TerritoryIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal virtual BusinessObjectList<EmployeeTerritory> EmployeeTerritoryList
		{
			get
			{
				BusinessObjectList<EmployeeTerritory> associatedEmployeeTerritoryList = null;
				//if (associatedEmployeeTerritoryList == null)
				//{
					associatedEmployeeTerritoryList = new BusinessObjectList<EmployeeTerritory>();
					foreach (DomainEmployeeTerritory EmployeeTerritory in base.GetChildRows(ParentCol.TerritoryEmployeeTerritoryRelation))
					{
						if (EmployeeTerritory.RowState != DataRowState.Deleted)
							associatedEmployeeTerritoryList.Add(new EmployeeTerritory(EmployeeTerritory));
					}
					associatedEmployeeTerritoryList.ObjectAdded += new BusinessObjectEventHandler(AssociatedEmployeeTerritoryList_ObjectAdded);
					associatedEmployeeTerritoryList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedEmployeeTerritoryList_ObjectRemoved);
				//}
				return associatedEmployeeTerritoryList;
			}
		}

		internal virtual DomainRegion RegionItem
		{
			get { return (DomainRegion)base.GetParentRow(this.ParentCol.RegionTerritoryRelation); }
			set
			{
				if (value != null)
				{
					DomainRegionCollection list = null;
					if (value.GetType() == typeof(DomainRegion))
						list = (DomainRegionCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.RegionCollection);
					bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
					this.ParentCol.SubDomain.EnforceConstraints = false;
					DomainRegion region = (DomainRegion)value;
					if(region.ParentCol != list)
					{
						this.ParentCol.SubDomain.Merge(new DataRow[] { region }, false, MissingSchemaAction.Error);
						region = list.GetRegion(region.RegionId);
					}
					base.SetParentRow(region, this.ParentCol.RegionTerritoryRelation);
					this.ParentCol.SubDomain.EnsureParentRowsExist();				
					this.ParentCol.SubDomain.EnforceConstraints = oldEc;
				}
				else
				{
					if(this.ParentCol.RegionIdColumn.AllowDBNull)
					{
						base.SetNull(this.ParentCol.RegionIdColumn);
					}
					else
					{
						this.Delete();
					}
				}
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainTerritory)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.TerritoryCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveTerritory(this);
		}

		#endregion

		#region Relationship Methods
		private void AssociatedEmployeeTerritoryList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainEmployeeTerritory addedItem = (DomainEmployeeTerritory)e.BusinessObject.WrappedClass;
			DomainEmployeeTerritoryCollection existingEmployeeTerritoryCollection = (DomainEmployeeTerritoryCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.EmployeeTerritoryCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingEmployeeTerritoryCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingEmployeeTerritoryCollection.GetEmployeeTerritory(addedItem.EmployeeId, addedItem.TerritoryId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.SetParentRow(this,this.ParentCol.TerritoryEmployeeTerritoryRelation);
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedEmployeeTerritoryList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainEmployeeTerritory removedItem = (DomainEmployeeTerritory)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.Delete();
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		internal virtual void ReleaseNonIdentifyingRelationships()
		{
		}

		#endregion
	}
}
